package com.hello.single;

import java.util.HashSet;
import java.util.Set;

public class test {


    public static void main(String[] args) {


    }

    /**
     * 测试懒汉模式的多线程并发问题
     * @throws InterruptedException
     */
    public void test1() throws InterruptedException {
        Set<LanhanSingle1> set = new HashSet<>();
        for (int i=0; i<1000; i++){
            // 并发获取实例
            new Thread(() -> {
                set.add(LanhanSingle1.getInstance());
            }).start();
        }
        // 等待10s
        Thread.sleep(10000);
        System.out.println("-----------");
        // 小概率出现多个实例
        for (LanhanSingle1 single: set){
            System.out.println(single);
        }
    }
}
